package servlet;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.ParticipationDao;
import DAO.StudyDao;
import DAO.UserDao;
import model.Category;
import model.Participation;
import model.Study;
import model.User;

public class StudyInfoAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		ActionForward forward=new ActionForward();
		Study study = new Study();
		ArrayList <Participation> participationList = new ArrayList <Participation>();
		StudyDao studyDao = new StudyDao();
		ParticipationDao participationDao = new ParticipationDao();
		UserDao userDao = new UserDao();
		
	HttpSession session = request.getSession();
		
		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userIdCheck = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
		if (userIdCheck < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}
		
	
			study = studyDao.getStudyInfo(studyId);
			participationList = participationDao.getSameStudyMember(studyId); 
			
			ArrayList<User> userList = new ArrayList<User>();
			
			for(int i=0; i<participationList.size();i++)
			{
			userList.add(userDao.getUserInfo(participationList.get(i).getUid()));
			}
			
			
			if(study != null) {
				request.setAttribute("study", study);
				
				if(userList != null)
				{
					request.setAttribute("userList", userList);
				}
				else{
					Util.ShowError(response, "팀원 정보가 존재하지 않습니다.");
				}

				forward.setPath("./detailStudyInfo.jsp");
			} 
				
	
		return forward;
	}

}
